package com.llh.workflow;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.soa.workflow.request.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author admin
 * @title 用于获取流程的主表/明细表数据
 * @description：用于节点后附加操作获取表单数据的工具类
 * @updateTime 2021/9/13 9:40
 * @throws
 */
public class WfFormDataUtil {
    private static BaseBean logger = new BaseBean();
    private static RequestInfo request;

    public WfFormDataUtil() {

    }

    public WfFormDataUtil(RequestInfo request) {
        this.request = request;
    }

    public static String Html2Text(String inputString) {
        String htmlStr = inputString; // 含html标签的字符串
        String textStr = "";
        Pattern p_script;
        Matcher m_script;
        Pattern p_style;
        Matcher m_style;
        Pattern p_html;
        Matcher m_html;
        Pattern p_html1;
        Matcher m_html1;
        try {
            String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
            // }
            String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
            // }
            String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
            String regEx_html1 = "<[^>]+";
            p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
            m_script = p_script.matcher(htmlStr);
            htmlStr = m_script.replaceAll(""); // 过滤script标签

            p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
            m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll(""); // 过滤style标签

            p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); // 过滤html标签

            p_html1 = Pattern.compile(regEx_html1, Pattern.CASE_INSENSITIVE);
            m_html1 = p_html1.matcher(htmlStr);
            htmlStr = m_html1.replaceAll(""); // 过滤html标签

            textStr = htmlStr;
            textStr = textStr.replaceAll(" ", "");
            textStr = textStr.replaceAll("\\r\\n", "");
            textStr = textStr.replaceAll("&nbsp;", "");
            // bs.writeLog("textStr:"+textStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return textStr;// 返回文本字符串
    }

    /**
     * 获取fieldid
     *
     * @param billid
     * @param filedname
     * @return
     */
    public static String getFiledIdByFieldName(String billid, String filedname) {
        RecordSet rs = new RecordSet();
        String fieldid = "";
        String sql = "select  id  from  workflow_billfield wb  where billid =? and fieldname =?";
        rs.executeQuery(sql, billid, filedname);
        if (rs.next()) {
            fieldid = Util.null2String(rs.getString("id"));
        }
        return fieldid;
    }

    /**
     * @param billid      表单id
     * @param filename    字段名称
     * @param selectvalue 汉字的值（显示值）
     * @return 下拉框数据库保存的值
     */
    public static String getSelectName(String billid, String filename, String selectvalue) {
        String selectname = "";
        RecordSet rs = new RecordSet();
        String sql = "select  a.selectname from workflow_selectitem a left join workflow_billfield b on a.fieldid =b" +
                ".id where  b.billid=? and b.fieldname=?  and a.selectvalue=?";
        rs.executeQuery(sql, billid, filename, selectvalue);
        if (rs.next()) {
            selectname = Util.null2String(rs.getString("selectname"));
        }

        return selectname;
    }

    /**
     * @return 主表id /明细的mainid
     * @Description: 获取主表的id即是明细表的mainid
     */
    public String getMainId() {
        String mainid = "";
        //主表名称
        String billtablename = request.getRequestManager().getBillTableName();
        String requestid = request.getRequestid();
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from " + billtablename + " where requestid=?", requestid);
        if (rs.next()) {
            mainid = Util.null2String(rs.getString("id"));
        }
        return mainid;
    }

    /**
     * @Description: 获取流程主表数据信息
     * @param: 无
     * @return:主表信息map集合
     */
    public Map<String, Object> getMainTableDatas() {
        Map<String, Object> map = new HashMap<String, Object>();
        // 获取表单主字段信息
        Property[] properties = (Property[]) request.getMainTableInfo().getProperty();
        for (int i = 0; i < properties.length; i++) {
            // 主字段名称
            String name = properties[i].getName();
            // 主字段对应的值
            String value = Util.null2String(properties[i].getValue());
            map.put(name, value);
        }
        logger.writeLog("获取到的主表数据信息==" + map.toString());
        return map;
    }

    /**
     * @return 明细表数据库list——> map   键名为字段明   值为字段值
     * @Title: getDetailTableInfo
     * @Description: 获取指定明细表信息
     * @param: d 指定的明细表   明细表1  及传入1
     * @return: List
     */
    public List<Map<String, Object>> getDetailTableDatas(int d) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        DetailTable[] detailtable = request.getDetailTableInfo().getDetailTable();
        int i = d - 1;
        if (detailtable.length > 0) {
            // 指定获取哪个明细表的数据
            DetailTable dt = detailtable[i];
            //当前明细表的行数
            Row[] s = dt.getRow();
            // 循环明细表的每一行
            for (int j = 0; j < s.length; j++) {
                HashMap<String, Object> detailMap = new HashMap<String, Object>();
                Row r = s[j];// 指定行
                Cell c[] = r.getCell();// 每行数据再按列存储
                for (int k = 0; k < c.length; k++) {
                    Cell c1 = c[k];// 指定列
                    String name = c1.getName();// 明细字段名称
                    String value = c1.getValue();// 明细字段的值
                    detailMap.put(name, value);
                }
                list.add(detailMap);
            }
        }
        logger.writeLog("==获取到的明细表数据信息==" + list.toString());
        return list;
    }
}
